<!--
 * @Author: blak-kong
 * @Date: 2019-12-12 11:56:13
 * @Email: 546598185@qq.com
 -->
<template>
  <div class="recom-cp">
        <el-button type="primary" icon="el-icon-plus" @click="add">新增</el-button>
        <el-table
            :data="tableData"
            border
            style="width: 100%;margin:10px auto;">
            <el-table-column
                label="名称"
                width="200">
                <template slot-scope="scope">
                    <el-popover trigger="hover" placement="top">
                        <p>点击进行编辑</p>
                        <div slot="reference" class="name-wrapper">
                            <!-- <p contenteditable>{{scope.row.name}}</p> -->
                            <el-input
                                placeholder="请输入内容"
                                v-model="scope.row.name">
                            </el-input>
                        </div>
                    </el-popover>
                </template>
            </el-table-column>
               <el-table-column
                label="描述">
                <template slot-scope="scope">
                    <el-popover trigger="hover" placement="top">
                        <p>点击进行编辑</p>
                        <div slot="reference" class="name-wrapper">
                            <el-input
                                type="textarea"
                                :rows="2"
                                placeholder="请输入内容"
                                v-model="scope.row.description">
                            </el-input>
                        </div>
                    </el-popover>
                </template>
            </el-table-column>
            <el-table-column
                width="120"
                label="图片">
                <template slot-scope="scope">
                    <el-upload
                        @click="tableIndex = scope.row.$index"
                        class="avatar-uploader"
                        :action="process.env.VUE_APP_AJAX + '/api/upload'"
                        :show-file-list="false"
                        :on-success="handleUploadSuccess"
                        :before-upload="beforeUploadUpload">
                        <img v-if="scope.row.img" :src="scope.row.img" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </template>
            </el-table-column>
            <el-table-column
                width="200"
                label="编辑">
                <template slot-scope="scope">
                    <el-button type="danger" size="medium" @click="handleDelete(scope.row, scope.row.$index)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="footer-btn">
            <el-button type="primary" @click="allSave">保存全部</el-button>
        </div>
  </div>
</template>

<script>
import { uuid, imageType } from '../utils/mUtils'
export default {
    name:"recom-cp",
    props: {
        tabName: String,
        tableData: Array
    },
    data() {
        return {
            tableIndex: 0
        }
    },
    created() {

    },
    mounted() {

    },
    methods: {
        // 统一格式: 事件名/参数/tab名 this.$emit("", obj, this.tabName)
        allSave() {
             this.$emit("all-save")
        },
        add() {
             this.$emit("add", {
                id: uuid(),
                name: "点击修改",
                description: "我是描述",
                img: ""
            }, this.tabName)
        },
        deleteData(id,index) {
            this.$emit("deleteData", {id, index}, this.tabName)
        },
        deleteRow(index) {
            this.$emit("deleteRow", {index}, this.tabName)
        },
        handleDelete(item, index) {
            if (item._id) {
                this.deleteData(item._id, index)
            } else {
                this.deleteRow(index)
            }
        },

        handleUploadSuccess(res, file) {
            this.$emit("handle-upload-success", {res, tableIndex}, this.tabName)
            // this.tableData[this.tableIndex].img = `http://localhost:5001/uploads/${res.filename}`
        },

        beforeUploadUpload(file) {
            const isIMG = imageType(file.type);
            const isLt2M = file.size / 1024 / 1024 < 2;

            this.imgType = isIMG
            if (!isIMG) {
                this.$message.error('文件格式错误!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isIMG && isLt2M;
        },
    }
}
</script>

<style>
.recom-cp {
    padding: 20px;
}
.footer-btn {
    display: flex;
    justify-content: center;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 80px;
    height: 80px;
    line-height: 80px !important;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 4px;
  }
  .avatar {
    width: 80px;
    height: 80px;
    display: block;
  }
</style>